package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired(required = false)
    private ItemMapper itemMapper;

    /**
     * 分页查询商品
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> itemPage(Long page, Long size) {
        //1. 设置当前页与页面大小
        Page<Item> pageDb = new Page<>(page, size);
        //2. 创建分页需要条件
        QueryWrapper queryWrapper = new QueryWrapper();
        //3. 调用selectPage方法实现分页，分页的结果会被封装到Page对象中
        itemMapper.selectPage(pageDb, queryWrapper);
        //4.把数据封装到PageDTO中返回
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        itemPageDTO.setTotal(pageDb.getTotal());
        itemPageDTO.setList(pageDb.getRecords());
        return itemPageDTO;
    }

    @Override
    public void add(Item item) {
        itemMapper.insert(item);
    }

    @Override
    public void updateStatusById(Long itemId, Integer itemStatus) {
        Item item = itemMapper.selectById(itemId);
        item.setStatus(itemStatus);
        itemMapper.updateById(item);
    }

    @Override
    public void deleteById(Long itemId) {
        itemMapper.deleteById(itemId);
    }
}
